package com.nightpiter.view.drawer.rapid;

import java.util.ArrayList;
import java.util.HashMap;

import com.nightpiter.model.objects.Transition;
import com.nightpiter.model.objects.Tunnel;
import com.nightpiter.view.graphics.Canvas;
import com.nightpiter.view.graphics.PointF;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
class Link {

	private static final LinkDrawerManager manager = new LinkDrawerManager();
	
	/** a set of transitions represented in this Link instance */
	private HashMap<PointF, Transition> transitionsMap = new HashMap<PointF, Transition>();

	protected Node evenNode, oddNode;

	/** a set of colors */
	protected ArrayList<Integer> colors = new ArrayList<Integer>();

	protected PointF evenPointF, oddPointF;

	protected boolean smooth;
	
	public Link(Node evenNode, Node oddNode) {
		this.evenNode = evenNode;
		this.oddNode = oddNode;
		this.evenPointF = evenNode.getPointF();
		this.oddPointF = oddNode.getPointF();
		evenNode.addNeighbor(oddNode, this);
		oddNode.addNeighbor(evenNode, this);
	}

	public void addTransition(Transition transition) {
		for (Transition eachTransition : transitionsMap.values()) {
			if (eachTransition.getFromStation() == transition.getToStation()
					&& eachTransition.getToStation() == transition
							.getFromStation()) {
				if (eachTransition instanceof Tunnel
						&& transition instanceof Tunnel) {
					if (((Tunnel) eachTransition).getLine() == ((Tunnel) transition)
							.getLine()) {
						return;
					}
				}
			}
		}
		this.transitionsMap.put(new PointF(), transition);
		if (transition instanceof Tunnel) {
			Tunnel tunnel = (Tunnel) transition;
			this.smooth = tunnel.isSmooth();
			this.colors.add(tunnel.getLine().getColor());
		}
	}

	public void draw(Canvas canvas) {
		manager.getDrawer().draw(canvas, this);
	}
	
	public static LinkDrawerManager getManager() {
		return manager;
	}

}
